package yang.service.impl;

import java.util.List;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import yang.dao.BaseDaoI;
import yang.model.DrugType;
import yang.model.HospitalUser;
import yang.service.InitServiceI;
import yang.util.Encrypt;


@Service("initService")
public class InitServiceImpl implements InitServiceI {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(InitServiceImpl.class);
	private BaseDaoI<DrugType> drugTypeDao;
	private BaseDaoI<HospitalUser> userDao;
	
	private static final String [] DRUGTYPES=new String []{"西药","中药","中成药"};
	@Override
	public void init() {
		initUser();
		initDrugType();
	}
	/**
	 * 初始化用户
	 */
	private void initUser(){
		List<HospitalUser> list=userDao.find("from HospitalUser");
		if (list.size()==0) {
			logger.info("准备初始化用户信息表");
			HospitalUser user=new HospitalUser();
			user.setUserNo(0);
			user.setUserNicheng("admin");
			user.setUserPwd(Encrypt.md5("admin"));
			user.setUserRole("管理员");
			logger.info("开始初始化");
			try {
				userDao.save(user);
				logger.info("初始化成功");
			} catch (Exception e) {
				logger.info("初始化失败");
				e.printStackTrace();
				
			}
		}
		
	}
	/**
	 * 初始化药品类型
	 */
	private void initDrugType(){
		List<DrugType> list =drugTypeDao.find("from DrugType");
		if (list.size()==0) {
			logger.info("开始初始化药品类型");
			for (String item  : DRUGTYPES) {
				DrugType type=new DrugType();
				type.setId(UUID.randomUUID().toString());
				type.setTypeName(item);
				try {
					drugTypeDao.saveOrUpdate(type);
					logger.info("药品类型初始化成功");
				} catch (Exception e) {
					logger.info("药品类型初始化失败");
					e.printStackTrace();
				}
			}
		}
		
		
	
	}
	

	public BaseDaoI<HospitalUser> getUserDao() {
		return userDao;
	}
	@Autowired
	public void setUserDao(BaseDaoI<HospitalUser> userDao) {
		this.userDao = userDao;
	}
	public BaseDaoI<DrugType> getDrugTypeDao() {
		return drugTypeDao;
	}
	@Autowired
	public void setDrugTypeDao(BaseDaoI<DrugType> drugTypeDao) {
		this.drugTypeDao = drugTypeDao;
	}

	
	
	
	

}
